﻿using System;
using System.Web.UI;
using Coolite.Ext.Web;
using Pap.Core.Utilities;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class UnionDepositAssociation : BasePage<UnionDepositAssociationViewModel>, IPermittedPage
    {

        /// <summary>
        /// Gets the committee ID.
        /// </summary>
        private string CommitteeID
        {
            get
            {
                var str = Request.QueryString["CommitteeID"];
                if (str == null)
                    return RegistryInfo.CommitteeID.ToString();
                return str;
            }
        }

        #region [ Events ]

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Ext.IsAjaxRequest && !Page.IsPostBack)
            {
                storeAssociations.DataSource = this.ViewModel.GetAllCommittees();
                storeAssociations.DataBind();
                storeProjects.DataSource = this.ViewModel.GetUserProjects();
                storeProjects.DataBind();

                RefreshData();

                if (!CommitteeID.IsNullOrEmpty())
                {
                    cmbAssociations.SelectedItem.Value = CommitteeID;
                    cmbAssociations_Selected(null, null);
                }
            }
        }

        /// <summary>
        /// Handles the Selected event of the cmbAssociations control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void cmbAssociations_Selected(object sender, AjaxEventArgs e)
        {
            GetMainAssociationDeposites(cmbAssociations.SelectedItem.Value.ToGuid());
        }

        /// <summary>
        /// Handles the Clicked event of the BtnDeposit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnDeposit_Clicked(object sender, AjaxEventArgs e)
        {
            this.ViewModel.DepositCommittee(cmbAssociations.SelectedItem.Value.ToGuid(), cmbProjects.SelectedItem.Value.ToGuid(), Convert.ToDouble(txtDepositAmount.Text),
                Convert.ToDateTime(dtDepositeDate.Value), txtOperationNumber.Text, InsertAttchments1.GetInsertedIDs(), InsertAttchments1.GetDeletedIDs());
            RefreshData();
            this.uxNewDepositWindow.Hide();
        }

        /// <summary>
        /// Handles the Clicked event of the btnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="args">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void btnBack_Clicked(object sender, AjaxEventArgs args)
        {
            this.RedirectToPreviousPage();
        }

        protected void btnNewDeposit_Click(object sender, AjaxEventArgs args)
        {
            this.uxNewDepositWindow.Show();
        }

        protected void btnCancel_Click(object sender,AjaxEventArgs args)
        {
            this.uxNewDepositWindow.Hide();
        }

        #endregion

        #region Functions

        /// <summary>
        /// Refreshes the data.
        /// </summary>
        private void RefreshData()
        {
            txtBalanceAfterDeposit.Reset();
            txtDepositAmount.Reset();
            txtOperationNumber.Reset();
            dtDepositeDate.Reset();
            txtDepositAmount.Reset();

            double unionBalance = this.ViewModel.GetMainCommitteeBalance();
            txtBalanceBeforeDeposit.Text = unionBalance.ToString();
            if (unionBalance > 0)
                txtDepositAmount.MaxValue = unionBalance;
            else
                btnDeposit.Disabled = true;

            if (!cmbAssociations.SelectedItem.Value.ToString().IsNullOrEmpty())
                GetMainAssociationDeposites(cmbAssociations.SelectedItem.Value.ToGuid());
        }

        /// <summary>
        /// Gets the main association deposites.
        /// </summary>
        /// <param name="committeeID">The committee ID.</param>
        private void GetMainAssociationDeposites(Guid committeeID)
        {
            storeAssociationDeposits.DataSource = this.ViewModel.GetMainAssociationDeposits(committeeID);
            storeAssociationDeposits.DataBind();
        }

        #endregion

        public string PermissionName
        {
            get { return Permissions.UnionDepositAssociation.ToString(); }
        }
    }
}
